package kdd.feature;

import java.sql.*;
import java.util.HashMap;

import kdd.lib.Lib;



public class DonationAbstraction {
	Connection conn;
		
	public DonationAbstraction(Connection conn2){
		conn = conn2;
	}
	
	private Boolean isUniqueMessage(String x){
		return x != null;
	}
	
	
	public double getGreatMessageProportion(String projectid) throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT donation_message FROM donations WHERE projectid=?");
		query.setString(1, projectid);
		ResultSet result = query.executeQuery();
		int ret = 0, count = result.getFetchSize();
		while (result.next())
			if (isUniqueMessage(result.getString(1))) ret ++;
		result.close();
		return ret * 100.0 / count;
	}
	
	public int getNumberOfNonTeacherDonorsDonatedOver100(String projectid) throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT count(*) FROM donations WHERE projectid=? AND is_teacher_acct='f' AND dollar_amount >= 100");
		query.setString(1, projectid);
		ResultSet result = query.executeQuery();
		if (result.next())
			return result.getInt(1);
		Lib.assertTrue(false);
		return 0;
	}
	
	public HashMap<String, Object> getTotalTeacherDonorNum() throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT projectid, COUNT(*) FROM donations WHERE is_teacher_acct='t' GROUP BY projectid;");
		HashMap<String, Object> ret = new HashMap<String, Object>();
		ResultSet result = query.executeQuery();
		while (result.next())
			ret.put(result.getString(1), result.getDouble(2));
		System.out.println("finished getTotalTeacherDonorNum");
		return ret;
	}
	
	public HashMap<String, Object> getTotalNonTeacherDonorNum() throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT projectid, COUNT(*) FROM donations WHERE is_teacher_acct='f'  GROUP BY projectid;");
		HashMap<String, Object> ret = new HashMap<String, Object>();
		ResultSet result = query.executeQuery();
		while (result.next())
			ret.put(result.getString(1), result.getDouble(2));
		System.out.println("finished getTotalNonTeacherDonorNum");
		return ret;
	}
	
	public HashMap<String, Object> getTotalNonTeacherDonorGiven100Num() throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT projectid, COUNT(*) FROM donations WHERE is_teacher_acct='f' AND dollar_amount >= 100 GROUP BY projectid;");
		ResultSet result = query.executeQuery();
		HashMap<String, Object> ret = new HashMap<String, Object>();
		while (result.next())
			ret.put(result.getString(1), result.getDouble(2));
		return ret;
	}
	
	public int getNumberOfThoughtfulDonor(String projectid){
		return 0;
	}
	
	public HashMap<String, Object> getTotalGreenDonation() throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT projectid, COUNT(*) FROM donations WHERE payment_included_acct_credit='t' GROUP BY projectid;");
		ResultSet result = query.executeQuery();
		HashMap<String, Object> ret = new HashMap<String, Object>();
		while (result.next())
			ret.put(result.getString(1), result.getDouble(2));
		System.out.println("finished getTotalGreenDonation");
		return ret;
	}
}
